﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour
{

    public Transform firePoint;
    public float force;
    //public float
    public float range=100f;

    public float fireRate = 15f;
    private float nextFire;

    void Start()
    {
        
    }


    void Update()
    {
        if (Input.GetKey(KeyCode.Space)&&Time.time>=nextFire)
        {
            nextFire = Time.time + 1f / fireRate;
            Shoot();
        }
    }

    private void Shoot()
    {
        Vector3 end = firePoint.forward.normalized * range;
        RaycastHit hit;
        if (Physics.Raycast(firePoint.position, firePoint.forward,out hit,range))
        {
            end = hit.point;
            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * force,ForceMode.Impulse);
            }
            Debug.Log(hit.transform.name);
        }
        Debug.DrawLine(firePoint.position,end,Color.red);
    }
}
